﻿using System;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class LoanCasesList : BasePage<LoanCasesListViewModel>, IPermittedPage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Ext.IsAjaxRequest)
            {
                storeLoanCases.DataSource = this.ViewModel.GetLoanCases();
                storeLoanCases.DataBind();
            }
        }

        /// <summary>
        /// Handles the Clicked event of the gridLoanCases control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void gridLoanCases_Clicked(object sender, AjaxEventArgs e)
        {
            if (e.ExtraParams["CommandName"] != null)
            {
                if (e.ExtraParams["CommandName"].Equals("PayAll"))
                {
                    if (!e.ExtraParams["LoanID"].IsNullOrEmpty())
                    {
                        double allDebts = this.ViewModel.GetLoanDebts((e.ExtraParams["LoanID"]).ToGuid());
                        txtAllDebts.Text = allDebts.ToString();
                        txtNumPaymentAmount.MinValue = allDebts;
                        txtNumPaymentAmount.MaxValue = allDebts;
                        wndPayAll.Show();
                    }
                }
                else
                    if (e.ExtraParams["CommandName"].Equals("CancelAllDebts"))
                    {
                        if (!e.ExtraParams["LoanID"].IsNullOrEmpty())
                            this.RedirectFromThisPage("CancelLoanDebts", new string[] { "LoanID=" + e.ExtraParams["LoanID"].ToString() });
                    }
            }
        }


        /// <summary>
        /// Handles the Clicked event of the BtnPayDebts control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnPayDebts_Clicked(object sender, AjaxEventArgs e)
        {
            RowSelectionModel rowSelected = gridLoanCases.SelectionModel.Primary as RowSelectionModel;
            this.ViewModel.PayForLoan(rowSelected.SelectedRow.RecordID.ToGuid(), Convert.ToDouble(txtNumPaymentAmount.Value));
            this.RedirectFromThisPage("LoanCasesList");
        }

        public string PermissionName
        {
            get { return Permissions.LoanCasesList.ToString(); }
        }
    }
}
